package com.ff.xyh.service.userfront.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.userfront.UserFavorite;
import com.ff.xyh.service.userfront.service.UserFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户收藏表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-25
 */
@RestController
@RequestMapping("/front-user")
public class UserFavoriteController {


    @Autowired
    private UserFavoriteService userFavoriteService;

    @RequestMapping("insert-user-favorite")
    public ResultObject insertUserFavorite(UserFavorite userFavorite) {

        if ( userFavoriteService.getOne(
                new QueryWrapper<UserFavorite>()
                        .eq("user_id", userFavorite.getUserId())
                        .eq("post_id", userFavorite.getPostId())
                        .eq("post_type", userFavorite.getPostType())) == null){
            if (userFavoriteService.save(userFavorite)) {
                return ResultObject.ok().message("成功收藏");
            } else {
                return ResultObject.error().message("收藏失败");
            }
        } else {
            return ResultObject.error().message("收藏失败, 请勿重复收藏");
        }
    }

    @RequestMapping("delete-user-favorite")
    public ResultObject deleteUserFavorite(
            @RequestParam(name = "id") String id,
            @RequestParam(name = "postId") String postId,
            @RequestParam(name = "postType") String postType ) {
        if (userFavoriteService.remove(
                new QueryWrapper<UserFavorite>()
                        .eq("user_id", id)
                        .eq("post_id", postId)
                        .eq("post_type", postType))) {
            return ResultObject.ok().message("成功取消收藏");
        } else {
            return ResultObject.error().message("取消收藏失败");
        }
    }

    /**
     * 删除用户所有收藏内容
     * 删除用户或用户注销时调用
     * @param id
     * @return
     */
    @RequestMapping("delete-user-favorite-id")
    public ResultObject deleteUserFavoriteById(
            @RequestParam(name = "id") String id ) {
        if (userFavoriteService.remove(
                new QueryWrapper<UserFavorite>()
                        .eq("user_id", id))) {
            return ResultObject.ok().message("成功删除该用户所有收藏信息");
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_DELETE);
        }
    }
}

